/**
 * Created by mar105 on 2017-11-14.
 */
import fetch from 'dva/fetch';
import React, { Component } from 'react';
import * as config from '../../utils/config';


export default (ChildComponent) => {
  return class Base extends Component {
    constructor(props) {
      super(props);
      this.state = {
        formId: props.sActiveId, // 获取配置数据用的modelId
        formData: [], // 获取配置数据
        gdsformconst: [], // 获取配置常量
        gdsjurisdiction: [], // 获取配置权限
        /* -----------------------------
                paramInfo: { dataId: '', srcTabId: '', srcComponentName: '' }
                dataId: 传递的数据id，
                srcTabId: 源单的tabid，后面用于数据更新同步刷新,
                srcComponentName:  源单的componentName，后面用于数据更新同步刷新
                ---------------------------- */
        pageLoading: true, // 整个页面loading
      };
    }

    componentDidMount() {
      if (this.state.formId !== undefined) {
        const url = `${config.server_host}business/getModelBysId/${this.state.formId}?sModelsId=${this.state.formId}`;
        const options = {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
          },
        };
        fetch(url, options).then(response => response.json()).then((json) => {
          if (json.code === 1) {
            console.log('111111', json.data);
          }
        });
      }
    }

    componentWillReceiveProps(nextProps) {
      const { dataList } = nextProps.content;
      const {
        sDataListTabId, formRoute, urlData, loading, returnType,
      } = dataList;
      if (this.state.sTabId === sDataListTabId
        && (this.state.sTabId === (Object.keys(nextProps.app.moreSelectPpopupPane !== undefined ? nextProps.app.moreSelectPpopupPane : '').length > 0 ? nextProps.app.moreSelectPpopupPane.key : ''))
        && this.state.formRoute === formRoute &&
        this.state.formData.length <= 0 &&
        this.state.gdsformconst.length <= 0 && this.state.gdsjurisdiction.length <= 0) {
        if (loading) {
          this.setState({ loading: true });
          return;
        }
        if (returnType !== undefined && returnType === 'CUD') {
          return;
        }
        const { formData, gdsformconst, gdsjurisdiction } = urlData;
        this.setState({
          formData, gdsformconst, gdsjurisdiction, pageLoading: false,
        });
      }
    }
    render() {
      return (<ChildComponent {...this.props} {...this.state} />);
    }
  };
};

